import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class QuestionDAOImpl implements QuestionDAO {

    @Override
    public Question findByQuestionNo(String questionNo) {
        try (Connection connection = DBUtil.getConnection()) {
            // 修改SQL语句，去掉多余的数据库名前缀
            String sql = "SELECT * FROM Question WHERE Qno =?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, questionNo);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                Question question = new Question();
                question.setQNo(resultSet.getString("Qno"));
                question.setQContent(resultSet.getString("Qcontent"));
                question.setChoiceA(resultSet.getString("ChoiceA"));
                question.setChoiceB(resultSet.getString("ChoiceB"));
                question.setQuestionType(resultSet.getString("Qtype"));
                return question;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Question> getAllQuestions() {
        List<Question> questions = new ArrayList<>();
        try (Connection connection = DBUtil.getConnection()) {
            // 修改SQL语句，去掉多余的数据库名前缀
            String sql = "SELECT * FROM Question";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Question question = new Question();
                question.setQNo(resultSet.getString("Qno"));
                question.setQContent(resultSet.getString("Qcontent"));
                question.setChoiceA(resultSet.getString("ChoiceA"));
                question.setChoiceB(resultSet.getString("ChoiceB"));
                question.setQuestionType(resultSet.getString("Qtype"));
                questions.add(question);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return questions;
    }

    @Override
    public void save(Question question) {
        try (Connection connection = DBUtil.getConnection()) {
            // 修改SQL语句，去掉多余的数据库名前缀
            String sql = "INSERT INTO Question (Qno, Qcontent, ChoiceA, ChoiceB, Qtype) VALUES (?,?,?,?,?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, question.getQNo());
            preparedStatement.setString(2, question.getQContent());
            preparedStatement.setString(3, question.getChoiceA());
            preparedStatement.setString(4, question.getChoiceB());
            preparedStatement.setString(5, question.getQuestionType());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void update(Question question) {
        try (Connection connection = DBUtil.getConnection()) {
            // 修改SQL语句，去掉多余的数据库名前缀
            String sql = "UPDATE Question SET Qcontent =?, ChoiceA =?, ChoiceB =?, Qtype =? WHERE Qno =?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, question.getQContent());
            preparedStatement.setString(2, question.getChoiceA());
            preparedStatement.setString(3, question.getChoiceB());
            preparedStatement.setString(4, question.getQuestionType());
            preparedStatement.setString(5, question.getQNo());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}